package com.kcsj.controller.basic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.kcsj.domain.Address;
import com.kcsj.service.AddressService;
import com.kcsj.util.JSONUtil;
import com.kcsj.util.MessageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
/**
 * @author 傅立贤
 * 28th Nov. 2020
 *
 */
@WebServlet("/address")
public class AddressController extends HttpServlet {
    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //将request body转换为JSON字串
        String req_json = JSONUtil.getJSON(request);
        try {
            //将请求转为JSONobject对象
            Address address = JSON.parseObject(req_json, Address.class);
            AddressService.getInstance().update(address);
            JSONObject message = MessageHelper.addMessage("地址信息已修改");
            response.getWriter().println(message);
        } catch (SQLException e) {
            e.printStackTrace();
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        }catch (Exception e){
            e.printStackTrace();
            JSONObject message = MessageHelper.netWordError();
            response.getWriter().println(message);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //将request body转换为JSON字串
        String req_json = JSONUtil.getJSON(request);
        if (req_json.equals("")) {
            JSONObject message = MessageHelper.addMessage("请输入地址信息");
            response.getWriter().println(message);
            return;
        }
        try {
            //将请求转为JSONobject对象
            Address address = JSON.parseObject(req_json, Address.class);
            AddressService.getInstance().add(address);
            JSONObject message = MessageHelper.addMessage("地址信息添加成功");
            response.getWriter().println(message);
        } catch (SQLException e) {
            JSONObject message = MessageHelper.dateBaseError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }catch (Exception e){
            JSONObject message = MessageHelper.netWordError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取前端请求的信息
        String customerId = request.getParameter("customerId");
        int customer_id = Integer.parseInt(customerId);
        try {
            this.responseAddress(response,customer_id);
        }catch (SQLException e){
            JSONObject message = MessageHelper.dateBaseError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }catch(Exception e){
            JSONObject message = MessageHelper.netWordError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取前端请求的信息
        String id = request.getParameter("id");
        int address_id = Integer.parseInt(id);
        try {
            //到数据库表中删除对应的教师
            AddressService.getInstance().delete(address_id);
            JSONObject message = MessageHelper.addMessage("删除成功");
            response.getWriter().println(message);
        } catch (SQLException e) {
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        } catch (Exception e) {
            JSONObject message = MessageHelper.netWordError();
            response.getWriter().println(message);
        }
    }

    //按查询条件响应学生对象
    private void responseAddress(HttpServletResponse response,Integer customerId)
            throws ServletException, IOException, SQLException {
        //获得所有学生
        Collection<Address> addresses =
                AddressService.getInstance().findAllByCustomerId(customerId);
        String addresses_json =
                JSON.toJSONString(addresses, SerializerFeature.DisableCircularReferenceDetect);
        JSONObject data = MessageHelper.addData(addresses_json);
        //响应departments_json到前端
        response.getWriter().println(data);
    }
}
